﻿//Write a program that finds the maximal sequence of equal elements in an array.
//Example: {2, 1, 1, 2, 3, 3, 2, 2, 2, 1} -> {2, 2, 2}.


namespace MaximalEqualSequenceOfElements
{
    using System;

    public class MaximalEqualSequenceOfElements
    {
        public static void Main(string[] args)
        {
            Console.Write("Enter array lengh: n = ");
            int elementsCount = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter {0} elements", elementsCount);
            int[] array = new int[elementsCount];
            for (int i = 0; i < elementsCount; i++)
            {
                array[i] = int.Parse(Console.ReadLine());
            }

            Console.WriteLine();

            int elementCount = 1;
            int elementIndex = 0;
            for (int indexOfElements = 0; indexOfElements < array.Length; indexOfElements++)
            {
                int tempVariable = 1;
                int tempIndex = 0;
                for (int i = indexOfElements + 1; i < array.Length; i++)
                {
                    if (array[indexOfElements] == array[i])
                    {
                        tempVariable++;
                        tempIndex = i;
                    }
                    else
                    {
                        break;
                    }
                }

                if (elementCount < tempVariable)
                {
                    elementCount = tempVariable;
                    elementIndex = tempIndex;
                }
            }

            Console.WriteLine("Element '{0}' appears {1} times from index {2} to index {3} ",
                array[elementIndex], elementCount, elementIndex - elementCount + 2, elementIndex + 1);
            Print(array, 0, array.Length - 1);
            Console.Write(" -> ");
            Print(array, elementIndex - elementCount + 1, elementIndex);
            Console.WriteLine();
        }

        private static void Print(int[] array, int start, int end)
        {
            for (int i = start; i <= end; i++)
            {
                if (i == start)
                {
                    Console.Write("{{{0}, ", array[i]);
                }
                else if (i == end)
                {
                    Console.Write("{0}}}", array[i]);
                }
                else
                {
                    Console.Write("{0}, ", array[i]);
                }
            }
        }
    }
}